package 数组.easy;

/**
 * @author Lw中
 * @date 2020/8/5 9:32
 */
public class LuLingWu {

    public static boolean canPlaceFlowers(int[] flowerbed, int n) {
        int sum = 0;
        for (int i = 0; i < flowerbed.length; i++) {
            if (flowerbed.length == 1) {
                if (flowerbed[i] == 0) {
                    flowerbed[i] = 1;
                    ++sum;
                }
            } else {
                if (i == 0) {
                    if (flowerbed[i+1] == 0 && flowerbed[i] == 0) {
                        flowerbed[i] = 1;
                        ++sum;
                    }
                } else if (i == flowerbed.length - 1) {
                    if (flowerbed[i-1] == 0 && flowerbed[i] == 0) {
                        flowerbed[i] = 1;
                        ++sum;
                    }
                } else {
                    if (flowerbed[i-1] == 0 && flowerbed[i+1] == 0 && flowerbed[i] == 0) {
                        flowerbed[i] = 1;
                        ++sum;
                    }
                }
            }
        }
        if (sum < n) {
            return false;
        } else {
            return true;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1};
        System.out.println(canPlaceFlowers(arr, 0));
    }

}
